package com.orhon.smartcampus.modules.systemctl.fetcher;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.orhon.smartcampus.modules.systemctl.entity.Operations;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLOperation;
import com.orhon.smartcampus.modules.systemctl.mm.OperationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.systemctl.dao.ModulesDao;
import com.orhon.smartcampus.modules.systemctl.entity.Menus;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLMenus;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.mm.MenuMapper;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class ModulesFetcher implements DataFetcher {

	@Autowired
	ModulesDao modulesDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		List<GQLModule> retData = new ArrayList<>();
		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);

		String hql = "from Modules where deleted_at=null ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));

			if(map.get("category")!=null) {
				hql += "and category "+ Util.decide(map.get("category"));
			}

			if(map.get("client")!=null) {
				hql += "and  clients " + Util.decide(map.get("client"));
			}
			if(map.get("mark")!=null) {
				hql += "and mark "+ Util.decide(map.get("mark"));
			}

			if(map.get("status")!=null) {
				hql += "and  status " + Util.decide(map.get("status"));
			}

		}

		List<Modules> dataList = modulesDao.find( hql, null , page.getPage() , page.getLimit());
		Long total = modulesDao.getTotalCount(hql);
		dataList.forEach(modules -> {


			List<GQLMenus> gqlMenus = null;
			if (env.getSelectionSet().contains("data/menus")){
				modules.getMenus().size();
				List<Menus> menus = modules.getMenus();
				gqlMenus = MenuMapper.MAPPER.toMenusList(menus);

			}
			List<GQLSchool> gqlSchool = null;
			if (env.getSelectionSet().contains("data/schools")){
				modules.getSchools().size();
				List<Schools> schools = modules.getSchools();
				gqlSchool = SchoolMapper.MAPPER.toSchoolList(schools);

			}
			List<GQLOperation> gqlOperation = null;
			if (env.getSelectionSet().contains("data/oprations")){
				List<Operations> operations = modules.getOprations();
				gqlOperation = OperationMapper.MAPPER.toOperationsList(operations);

			}
			GQLModule module = ModuleMapper.MAPPER.mapModule(modules);
			module.setMenus(gqlMenus);
			module.setSchools(gqlSchool);
			module.setOprations(gqlOperation);
			retData.add(module);
		});
		return Result.ok().put("data" , retData).put("total" , total);
	}



}
